﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Autofac;
using DevExpress.XtraBars;
using DevExpress.XtraBars.Ribbon;
using DevExpress.XtraSplashScreen;
using DevExpress.XtraTab;
using Quartz;
using Quartz.Impl;
using SqlSugar;
using StackExchange.Redis;
using WinformDevelopFrame.Common;
using WinformDevelopFrame.Core.Extend;
using WinformDevelopFrame.Core.Form;
using WinformDevelopFrame.Core.Quartz;
using WinformDevelopFrame.Entity;
using WinformDevelopFrame.IServices;
using WinformDevelopFrame.UI;

namespace WinformDevelopFrame
{
    public partial class FrmMain : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        private IsysmenuService _sysmenuService;
        private IsysuserService _sysuserService;
        private ISqlSugarClient _sqlSugarClient;
        public FrmMain(IsysmenuService sysmenuService,IsysuserService sysuserService,ISqlSugarClient sqlSugarClient)
        {
            _sysmenuService = sysmenuService;
            _sysuserService = sysuserService;
            _sqlSugarClient=sqlSugarClient;
            InitializeComponent();
        }

        private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            ChildWinManagement.LoadMdiForm(xtraTabControl1, AppInfo.Container.ResolveNamed<Form>("FrmGeneratedCode"));
        }

        private void barButtonItem2_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            ChildWinManagement.LoadMdiForm(xtraTabControl1, AppInfo.Container.ResolveNamed<Form>("Frmsysmenu"));
        }

        private void barButtonItem3_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            ChildWinManagement.LoadMdiForm(xtraTabControl1, AppInfo.Container.ResolveNamed<Form>("Frmsysdatatable"));
        }

        private void ceshi_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            ChildWinManagement.LoadMdiForm(xtraTabControl1, AppInfo.Container.ResolveNamed<Form>("FrmTest"));
        }

        private void barButtonItem8_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {

        }

        private void ribbonControl1_Click(object sender, EventArgs e)
        {

        }

        private void FrmMain_Load(object sender, EventArgs e)
        {
            var menus = AppInfo.Menus;
            foreach (var menu in menus)
            {
                if (menu.MenuType.Equals("Page")&&AppInfo.Menus.Select(p=>p.MenuCode).Contains(menu.MenuCode))
                {
                    this.ribbonControl1.Pages.Add(new RibbonPage(menu.MenuText){Name = menu.MenuCode,Tag = menu.Id});
                }
            }
            foreach (var menu in menus)
            {
                if (menu.MenuType.Equals("Group") && AppInfo.Menus.Select(p => p.MenuCode).Contains(menu.MenuCode))
                {
                    this.ribbonControl1.Pages.FirstOrDefault(p=>p.Tag.Equals(menu.PId))?.Groups.Add(new RibbonPageGroup(menu.MenuText){ Name = menu.MenuCode, Tag = menu.Id });
                }
            }

            foreach (var menu in menus)
            {
                if (menu.MenuType.Equals("Form") && AppInfo.Menus.Select(p => p.MenuCode).Contains(menu.MenuCode))
                {
                    foreach (RibbonPage ribbonControl1Page in this.ribbonControl1.Pages)
                    {
                        BarButtonItem btn = new BarButtonItem()
                        { Caption = menu.MenuText, Name = menu.MenuName, Tag = menu.Id };
                        btn.ItemClick += (sender, o) =>
                        {
                            try
                            {
                                SplashScreenManager.ShowForm(typeof(FrmLoading));
                                Form form = null;
                                if (menu.IsExtendDevelop)
                                {
                                    form = (System.Windows.Forms.Form)Activator.CreateInstance(Assembly.LoadFrom(Application.StartupPath + menu.MenuDll).GetType(menu.MenuName));
                                }
                                else
                                {
                                    form = AppInfo.Container.ResolveNamed<Form>(btn.Name);

                                }
                                form.Tag = menu.Id;
                                if (menu.ShowType.Equals("ShowDialog"))
                                {
                                    form.ShowDialog();
                                }
                                else
                                {
                                    ChildWinManagement.LoadMdiForm(xtraTabControl1, form);
                                }
                                SplashScreenManager.CloseForm();
                            }
                            catch (Exception e)
                            {
                                SplashScreenManager.CloseForm();

                                throw;
                            }
                        };
                        if (!string.IsNullOrEmpty(menu.MenuIcon))
                        {
                            btn.LargeGlyph = Image.FromFile(Application.StartupPath + menu.MenuIcon);
                            btn.Glyph=Image.FromFile(Application.StartupPath + menu.MenuIcon);
                        }
                        ribbonControl1Page.Groups.FirstOrDefault(p => p.Tag.Equals(menu.PId))?.ItemLinks.Add(btn);
                    }
                }
            }

            barStaticItem1.Caption =
                "部门:"+_sqlSugarClient.Queryable<sysdept>().First(p => p.Id == AppInfo.User.DeptId)?.DeptName;
            barStaticItem2.Caption = "用户:" + AppInfo.User.UserName;
            barStaticItem3.Caption =
                "角色:" + _sqlSugarClient.Queryable<sysrole>().First(p => p.Id == AppInfo.User.RoleId)?.RoleName;
            barStaticItem5.Caption = "版本号:" + ConfigurationManager.AppSettings["Version"];
        }

        private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing)
            {
                if (DialogResult.Yes == "确定退出系统？".ShowYesNoAndTips())
                {
                    AppInfo.User.LoginStatus = LoginStatus.Logout;
                    _sysuserService.UpdateN(AppInfo.User);
                    Application.Exit();
                }
                else
                {
                    e.Cancel = true;
                }
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            Task.Run(() =>
            {
                barStaticItem4.Caption = DateTime.Now.ToString();
            });
        }

        private void skinDropDownButtonItem1_ItemClick(object sender, ItemClickEventArgs e)
        {
            string skinName = skinDropDownButtonItem1.Caption;
            DevExpress.LookAndFeel.UserLookAndFeel.Default.SetSkinStyle(skinName);
            Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            configuration.AppSettings.Settings["SkinName"].Value = skinName;
            configuration.Save(ConfigurationSaveMode.Modified);
        }

        private void xtraTabControl1_CloseButtonClick(object sender, EventArgs e)
        {
            DevExpress.XtraTab.ViewInfo.ClosePageButtonEventArgs EArg = (DevExpress.XtraTab.ViewInfo.ClosePageButtonEventArgs)e;
            string name = EArg.Page.Text;
            if (name.Equals("主页"))
            {
                return;
            }
            foreach (XtraTabPage page in xtraTabControl1.TabPages)  
            {
                if (page.Text == name)
                {
                    xtraTabControl1.TabPages.Remove(page);
                    page.Dispose();
                    return;
                }
            }
        }
    }
}
